#include <iostream>
#include <fstream>
#include <vector>
#include "windows.h"
#include <highgui.h>
#include <cxcore.h>
#include <cv.h>
#include <cvaux.h>

using namespace std;

int SearchDirectory(std::vector<std::string> &refvecFiles,
                    const std::string        &refcstrRootDirectory,
                    const std::string        &refcstrExtension,
                    bool                     bSearchSubdirectories = true);

int main(int argc,char* argv[])
{
	if(argc!=3) 
	{
		cerr<<"Please."<<endl;
		return -1;
	}
	std::string lCassifierFilename = argv[1];
	std::string lPath = argv[2];
	vector<std::string> lFiles;

	int lWhat = SearchDirectory(lFiles,lPath,"*",true);

	CvHaarClassifierCascade* lCascade = (CvHaarClassifierCascade*) cvLoad(lCassifierFilename.c_str());
	CvMemStorage* lMem = cvCreateMemStorage(0);

	int hit = 0;
	for(unsigned int i=0;i<lFiles.size();i++)
	{
		IplImage* lImg = cvLoadImage(lFiles[i].c_str());
		CvSeq* lObjects = cvHaarDetectObjects(lImg, lCascade, lMem, 1.1, 1, CV_HAAR_FIND_BIGGEST_OBJECT, lCascade->orig_window_size);
		if(lObjects->total>0)
			hit++;
	}

	cout<<"hit \t hit rate"<<endl;
	cout<<hit<<"\t"<<(float)hit/(float)lFiles.size()<<endl;
	
	system("pause");

	return 0;
}

int SearchDirectory(std::vector<std::string> &refvecFiles,
                    const std::string        &refcstrRootDirectory,
                    const std::string        &refcstrExtension,
                    bool                     bSearchSubdirectories)
{
	std::string     strFilePath;             // Filepath
	std::string     strPattern;              // Pattern
	std::string     strExtension;            // Extension
	HANDLE          hFile;                   // Handle to file
	WIN32_FIND_DATA FileInformation;         // File information


	strPattern = refcstrRootDirectory + "\\*.*";

	hFile = ::FindFirstFile(strPattern.c_str(), &FileInformation);
	if(hFile != INVALID_HANDLE_VALUE)
	{
		do
		{
			if(FileInformation.cFileName[0] != '.')
			{
				strFilePath.erase();
				strFilePath = refcstrRootDirectory + "\\" + FileInformation.cFileName;

				if(FileInformation.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
				{
					if(bSearchSubdirectories)
					{
						// Search subdirectory
						int iRC = SearchDirectory(refvecFiles,
						strFilePath,
						refcstrExtension,
						bSearchSubdirectories);
						if(iRC)
							return iRC;
					}
				}
				else
				{
					// Check extension
					strExtension = FileInformation.cFileName;
					strExtension = strExtension.substr(strExtension.rfind(".") + 1);

					if(strExtension == refcstrExtension || refcstrExtension == "*")
					{
						// Save filename
						refvecFiles.push_back(strFilePath);
					}
				}
			}
		} while(::FindNextFile(hFile, &FileInformation) == TRUE);

		// Close handle
		::FindClose(hFile);

		DWORD dwError = ::GetLastError();
		if(dwError != ERROR_NO_MORE_FILES)
			return dwError;
	}
}